package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.DishFlavorService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    // @Autowired 是spring框架的
    // @Resource 是javax.annotation的 JDK的 注意JDK9没有
    @Resource
    private DishMapper dishMapper;
    @Resource
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealDishMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void saveDish(DishDTO dishDTO) {
        //1. dishDTO里面的菜品属性赋值给Dish对象
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //2.执行菜品数据插入到数据库（插入的同时需要获取自增长的主键值）
        dish.setStatus(StatusConstant.ENABLE);
        dishMapper.insertDish(dish);

        //3.遍历口味列表，设置每个口味对象的逻辑外键dishId
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        if(dishFlavorList == null || dishFlavorList.size() == 0){
            throw new RuntimeException("请添加口味，不然我帮你口");
        }
        dishFlavorList.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });

        //执行菜品口味列表数据插入到数据库
        dishFlavorMapper.batchInsertDishFlavor(dishFlavorList);
    }


    /**
     * 分页查询菜品
     *
     * @param dishDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishDTO) {
        // 设置分页参数
        PageHelper.startPage(dishDTO.getPage(),dishDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<DishVO> page = dishMapper.pageQuery(dishDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    //事务
    @Transactional
    @Override
    public void deleteById(Long[] ids) {
        // 查询删除的菜品是否有在起售中的
        Integer count = dishMapper.selectCountByIdsAndStatus(ids, StatusConstant.ENABLE);
        if(count > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        // 查询删除的菜品是否有被套餐关联的
        Integer count2 = setmealDishMapper.selectCountByDishIds(ids);
        if(count2 > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //都没有，删除菜品的口味信息
        dishFlavorMapper.deleteByIds(ids);
        //删除菜品信息
        dishMapper.deleteByIds(ids);
    }
}
